using System;
using System.Globalization;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;
using System.Reflection;
using JigLibX.Physics;
using JigLibX.Collision;
using Services;
using Utilities.Menu.UIElements;
using BowMaster.Resources;
using Services.Menu;
using Utilities;

namespace BowMaster.Menu
{
    class LevelSelectMenuNoDrag : DrawableGameComponent
    {

        GraphicsDeviceManager GraphicsDeviceManager;
        InputManager input_manager;
        Texture2D level_bg, bg_text, upper_bg_box;//, frame;
        SpriteFont arial18, arial14;
        MyButton cancel; //title serve per la versione windows
        SpriteBatch spriteBatch;
        StateManager StateManager;
        ArrayList<MyButton> levels_num;
        //Vector2 titlePos;
        protected float press_wait;
        protected readonly float PRESS_INTERVAL = 0.5f;
        //Rectangle frame_rect;
        public LevelSelectMenuNoDrag(Game game, GraphicsDeviceManager graphics)
            : base(game)
        {
            this.GraphicsDeviceManager = graphics;
        }

        public override void Initialize()
        {
            spriteBatch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
            StateManager = Game.Services.GetService(typeof(AStateManager)) as StateManager;
            input_manager = new InputManager(Game);
            Game.Components.Add(input_manager);
            input_manager.OnTap += on_tap;
            base.Initialize();
        }
        int initial_x = 35;
        int initial_y = 150;
        int buttons_size = 60;
        int buttons_offset = 10;
        int columns = 6;


        Rectangle lowerBlack;
        Texture2D whiteTexture;
        
        //MyButton totalTime_b;
        MyButton status_b;
        
        MyButton title1, title2;

        protected override void LoadContent()
        {
            whiteTexture = Game.Content.Load<Texture2D>("Textures/whitePixel");
            

            //level_button_h = 50;
            //level_offset = 20;
            //final_y = levels_to_show * (buttons_h + level_offset) - level_offset + initial_y;
            //bar_height = levels_to_show * (buttons_h + level_offset) - level_offset;
            //buttonbg = Game.Content.Load<Texture2D>("Levels/whiteTexture");
            level_bg = Game.Content.Load<Texture2D>("Textures/whitePixel");
            //next_levels_t = Game.Content.Load<Texture2D>("Levels/nextLevels");
            //prevLevels_t = Game.Content.Load<Texture2D>("Levels/prevLevels");
            //bar = Game.Content.Load<Texture2D>("Levels/bar");
            //frame = Game.Content.Load<Texture2D>("Levels/frame");
            arial18 = Game.Content.Load<SpriteFont>("Fonts/Arial18Ptx");
            arial14 = Game.Content.Load<SpriteFont>("Fonts/Arial14Ptx");
            var emptyTexture = Game.Content.Load<Texture2D>("Textures/emptyPixel");
            //cancel = new MyButton(new Rectangle(0, 700, 480, 100), Strings.Menu_Back, Color.Blue, arial18, Color.White, Game.Content.Load<Texture2D>("Levels/back"));
            
            //title = new MyButton(new Rectangle(initial_x, 50, level_button_w, 70), Strings.Menu_SelectLevel, Color.Blue, arial18, Color.CornflowerBlue, buttonbg);

            levels_num = new ArrayList<MyButton>();
            //titlePos = new Vector2(240, 50) - arial18.MeasureString(title) / 2;
            var distance = (buttons_size + buttons_offset);
            for (int i = 0; i < StateManager.levels.Count; i++)
            {
                //float f = ((float)i) / StateManager.levels.Count;
                //var l = StateManager.levels[i];
                //var name = l.Name;
                //if (StateManager.GameState.Available[i] && StateManager.GameState.Records[i]<10000)
                //    name += "\n" + Strings.Menu_RecordTime + ": " + StateManager.GameState.Records[i];
                //var rect = new Rectangle(initial_x, initial_y + (level_button_h + level_offset) * i, level_button_w, level_button_h);
                //Vector2 tpos = centerString(name, arial18, rect);
                //tpos.X = 250;
                var r = i / columns;
                var c = i % columns;
                levels_num.Add(new MyButton(new Rectangle(initial_x + distance * c, initial_y + distance * r, buttons_size, buttons_size),
                    "" + (i + 1),
                    Color.White,
                    arial18,
                    TextPos.Center,
                    whiteTexture,
                    StateManager.GameState.Available[i] ? textColor : Color.Gray
                    ));
            }
            //prevLevels = new MyButton(new Rectangle(initial_x, initial_y - 20 - level_button_h / 2, level_button_w, level_button_h / 2), "", Color.White, null, Color.White, prevLevels_t);
            //nextLevels = new MyButton(new Rectangle(initial_x, initial_y + 20 + (level_button_h + level_offset) * levels_to_show-level_offset, level_button_w, level_button_h / 2), "", Color.White, null, Color.White, next_levels_t);
            //prevLevels = new MyButton(new Rectangle(initial_x, initial_y - 20 - buttons_h, level_button_w, buttons_h), "", Color.White, null, Color.White, prevLevels_t);
            //nextLevels = new MyButton(new Rectangle(initial_x, initial_y + 20 + (level_button_h + level_offset) * levels_to_show - level_offset, level_button_w, buttons_h), "", Color.White, null, Color.White, next_levels_t);
            //frame_rect = new Rectangle(initial_x - 20, initial_y - 20, 40 + level_button_w, 40 + (level_button_h + level_offset) * levels_to_show - level_offset);
            //velocity = 0;

            //bar_rect = new Rectangle(bar_x, initial_y, bar_width, bar_height);
            //indicator_rect = new Rectangle(bar_x, initial_y, bar_width, 10);

            //upperBlack = new Rectangle(0, 0, 480, initial_y);
            lowerBlack = new Rectangle(0, 800 - 90, 480, 160);

            int completed=0;
            float totaltime=0;

            for(int i=0;i<StateManager.GameState.Available.Count;i++)
                if (StateManager.GameState.Available[i] && StateManager.GameState.Records[i] <= 1000)
                {
                    completed++;
                    totaltime += StateManager.GameState.Records[i];
                }
            totaltime = (float)Math.Round(totaltime, 2);
            //status_b = new MyButton(new Rectangle(0, lowerBlack.Y + 10, 480, 40), Strings.Menu_Completed + completed + " / " + levels_num.Count, Color.White, arial18, Color.Black, whiteTexture);
            //totalTime_b = new MyButton(new Rectangle(0, status_b.rectangle.Y + 30, 480, 40), Strings.Menu_TotalTime + totaltime, Color.White, arial18, Color.Black, whiteTexture);

            status_b = new MyButton(new Rectangle(0, lowerBlack.Y + 10, 240, 40), Strings.Menu_Completed + completed + " / " + levels_num.Count, Color.White, arial18, TextPos.Center, whiteTexture, Color.Black);
            //totalTime_b = new MyButton(new Rectangle(0, lowerBlack.Y + 50, 240, 40), Strings.Menu_TotalTime + totaltime, Color.White, arial18, Color.Black, whiteTexture);
            cancel = new MyButton(new Rectangle(310, lowerBlack.Y + 20, 100, 50), Strings.Menu_Back, Color.White, arial18, TextPos.Center, Game.Content.Load<Texture2D>("Levels/bar_box"), Color.White);

            title1 = new MyButton(new Rectangle(0, 0, 480, 100), Strings.Menu_Title, textColor, Game.Content.Load<SpriteFont>("Fonts/Arial54Ptx"));
            title2 = new MyButton(new Rectangle(0, 70, 480, 100), Strings.Menu_SelectLevel, textColor, Game.Content.Load<SpriteFont>("Fonts/Arial42Ptx"));

            bg_text = Game.Content.Load<Texture2D>("Levels/bg_port");
            upper_bg_box = Game.Content.Load<Texture2D>("Levels/upper_bg_box");
            base.LoadContent();
        }
        protected override void Dispose(bool disposing)
        {
            Game.Services.RemoveService(typeof(IInputManager));
            base.Dispose(disposing);
        }


        public override void Update(GameTime gameTime)
        {
            var dt=(float)gameTime.ElapsedGameTime.TotalSeconds;
            if (press_wait < PRESS_INTERVAL)
                press_wait += dt;
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                StateManager.SelectMainMenu();
                return;
            }
            base.Update(gameTime);
        }
        Color textColor = new Color(8, 141, 217);
        Rectangle bg_rect = new Rectangle(0, 0, 480, 800);
        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            
            spriteBatch.Begin();

            spriteBatch.Draw(bg_text, bg_rect, Color.White);
            for (int i = 0; i < levels_num.Count; i++)
                    levels_num[i].Draw(spriteBatch);
                
            
            //spriteBatch.Draw(upper_bg_box, upperBlack, Color.White);
            spriteBatch.Draw(whiteTexture, lowerBlack, Color.Black);

            title1.Draw(spriteBatch);
            title2.Draw(spriteBatch);

            status_b.Draw(spriteBatch);
            


            cancel.Draw(spriteBatch);

            spriteBatch.End();
            base.Draw(gameTime);
        }
        Point tp = new Point();
        void on_tap(Vector2 tap_position)
        {
            if (press_wait < PRESS_INTERVAL) return;
            tp.X = (int)tap_position.X;
            tp.Y = (int)tap_position.Y;
            if (cancel.tapped_on(tp))
                StateManager.SelectMainMenu();
            else for (int i = 0; i < levels_num.Count; i++)
                    if (levels_num[i].tapped_on(tp) && StateManager.GameState.Available[i])
                    StateManager.SelectLevel(i);
        }
    }
}
